package com.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.dao.TCatelogDAO;
import com.dao.TGonggaoDAO;
import com.dao.TGoodsDAO;
import com.dao.TOrderItemDAO;
import com.dao.TXinwenDAO;
import com.model.TGoods;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class indexAction extends ActionSupport
{
	private TGonggaoDAO gonggaoDAO;
	private TXinwenDAO xinwenDAO;
	private TGoodsDAO goodsDAO;
	private TCatelogDAO catelogDAO;
	private TOrderItemDAO orderItemDAO;


	public TOrderItemDAO getOrderItemDAO() {
		return orderItemDAO;
	}


	public void setOrderItemDAO(TOrderItemDAO orderItemDAO) {
		this.orderItemDAO = orderItemDAO;
	}


	public TCatelogDAO getCatelogDAO() {
		return catelogDAO;
	}


	public void setCatelogDAO(TCatelogDAO catelogDAO) {
		this.catelogDAO = catelogDAO;
	}


	public TXinwenDAO getXinwenDAO() {
		return xinwenDAO;
	}


	public TGoodsDAO getGoodsDAO() {
		return goodsDAO;
	}


	public void setGoodsDAO(TGoodsDAO goodsDAO) {
		this.goodsDAO = goodsDAO;
	}


	public void setXinwenDAO(TXinwenDAO xinwenDAO) {
		this.xinwenDAO = xinwenDAO;
	}




	public TGonggaoDAO getGonggaoDAO()
	{
		return gonggaoDAO;
	}


	public void setGonggaoDAO(TGonggaoDAO gonggaoDAO)
	{
		this.gonggaoDAO = gonggaoDAO;
	}

	public String index()
	{
		Map request=(Map)ServletActionContext.getContext().get("request");
		Map session=ActionContext.getContext().getSession();
		
		String sql="from TGonggao order by id desc";
		List gonggaoList=gonggaoDAO.getHibernateTemplate().find(sql);
		if(gonggaoList.size()>4)
		{
			gonggaoList=gonggaoList.subList(0, 4);
		}
		session.put("gonggaoList", gonggaoList);
		
		
		String sql2 = "from TXinwen order by id desc";
		List xinwenList=xinwenDAO.getHibernateTemplate().find(sql2);
		if(xinwenList.size()>4)
		{
			xinwenList=xinwenList.subList(0, 4);
		}
		session.put("xinwenList", xinwenList);
		
		
		
		String sql3="from TGoods where goodsDel='no' and goodsIsnottejia='no' order by goodsId desc";
		List goodsNoTejiaList=goodsDAO.getHibernateTemplate().find(sql3);
		
		
		if(goodsNoTejiaList.size()>4)
		{
			goodsNoTejiaList=goodsNoTejiaList.subList(0, 4);
		}
		request.put("goodsNoTejiaList", goodsNoTejiaList);
		
		
		
		
		String sql4="from TCatelog where catelogDel='no'";
		List cateLogList=catelogDAO.getHibernateTemplate().find(sql4);
		session.put("cateLogList", cateLogList);
		
		
		
		/*List goodsListpaihang=new ArrayList();
		sql="select sum(goodsQuantity),goodsId from TOrderItem group by goodsId order by sum(goodsQuantity) desc";
		List list=orderItemDAO.getHibernateTemplate().find(sql);
		for(int i=0;i<list.size();i++)
		{
			Object[] b=(Object[])list.get(i);
			int goodsId=Integer.parseInt(b[1].toString());
			
			TGoods goods=goodsDAO.findById(goodsId);
			goodsListpaihang.add(goods);
		}
		if(goodsListpaihang.size()>4)
		{
			goodsListpaihang=goodsListpaihang.subList(0, 4);
		}
		request.put("goodsListpaihang", goodsListpaihang);*/
		
		return ActionSupport.SUCCESS;
	}

}
